package cn.hfatec.shms.web.members.ykManage.service;

import cn.hfatec.Page;
import cn.hfatec.Table;
import cn.hfatec.base.BaseDao;
import cn.hfatec.shms.advice.annotation.MysqlDataSource;
import cn.hfatec.shms.enums.KeyTypeEnum;
import cn.hfatec.shms.exception.KeyGenException;
import cn.hfatec.shms.system.user.model.User;
import cn.hfatec.shms.utils.KeyGenUtils;
import cn.hfatec.shms.web.members.ykBill.service.YkBillService;
import cn.hfatec.shms.web.members.ykLog.dao.YkLogDao;
import cn.hfatec.shms.web.members.ykLog.model.YkLog;
import cn.hfatec.shms.web.members.ykManage.dao.YkManageDao;
import cn.hfatec.shms.web.members.ykManage.model.YkManage;
import cn.hfatec.shms.web.members.ykManage.model.YkManageParams;
import cn.hfatec.shms.web.members.ykManage.model.YkManageView;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


/**
 * @author hfatec
 * @version 1.0
 *          <p/>
 *          the JAVA code is generate by middlegen
 */
@Service("YkManageService")
public class YkManageService {

    public static final String BEAN_ID = "YkManageService";

    //注入Dao层
    @Resource(name = "YkManageDao")
    private YkManageDao ykManageDao;

    //注入BaseDao
    @Resource(name = "BaseDao")
    private BaseDao baseDao;

    //注入Dao层
    @Resource(name = "YkLogDao")
    private YkLogDao ykLogDao;

    @Resource(name = "YkBillService")
    private YkBillService ykBillService;


    /**
     * 查询数据
     *
     * @param ykManageParams
     * @param page
     */
    @Transactional
    @MysqlDataSource
    public Table queryYkManageTable(YkManageParams ykManageParams, Page page) {

        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("params", ykManageParams);
        return baseDao.queryTableByPage("queryYkManage", params, page);
    }

    /**
     * 插入数据
     *
     * @param ykManage
     */
    @Transactional
    @MysqlDataSource
    public void saveYkManage(YkManage ykManage, User user) throws InvocationTargetException, IllegalAccessException, KeyGenException {
        if (ykManage.getId() != null) {
            YkManage result = getYkManage(ykManage.getId());
            if (result != null) {
                ykManageDao.update(ykManage);

                ykManage.setCreateTime(new Date());
                ykManage.setCreateUser(user.getLoginName());
                YkLog ykLog = new YkLog();
                BeanUtils.copyProperties(ykLog, ykManage);
                ykLogDao.insert(ykLog);
                ykBillService.updateYkBill(ykManage);
            } else {
                ykManage.setBillId(KeyGenUtils.createKeyByType(KeyTypeEnum.BILL_KEY));
                ykManage.setCreateTime(new Date());
                ykManage.setCreateUser(user.getLoginName());
                ykManageDao.insert(ykManage);
                YkLog ykLog = new YkLog();
                BeanUtils.copyProperties(ykLog, ykManage);
                ykLogDao.insert(ykLog);
                ykBillService.saveYkBill(ykManage);
            }
        } else {
            ykManage.setBillId(KeyGenUtils.createKeyByType(KeyTypeEnum.BILL_KEY));
            ykManage.setCreateTime(new Date());
            ykManage.setCreateUser(user.getLoginName());
            ykManageDao.insert(ykManage);
            YkLog ykLog = new YkLog();
            BeanUtils.copyProperties(ykLog, ykManage);
            ykLogDao.insert(ykLog);
            ykBillService.saveYkBill(ykManage);
        }
    }

    /**
     * 删除数据
     *
     * @param id
     */
    @Transactional
    @MysqlDataSource
    public void deleteYkManage(Integer id) {
        YkManage ykManage=getYkManage(id);
        ykBillService.deleteUpdateYkBill(ykManage);
        ykManageDao.delete(id);
    }

    /**
     * 删除数据
     *
     * @param ykManageList
     */
    @Transactional
    @MysqlDataSource
    public void deleteYkManageBatch(List<String> ykManageList) throws InvocationTargetException, IllegalAccessException {
        for (String id : ykManageList) {
            YkLog ykLog = new YkLog();
            YkManage ykManage=  getYkManage(Integer.valueOf(id));
            BeanUtils.copyProperties(ykLog, ykManage);
            ykLogDao.insert(ykLog);
            deleteYkManage(Integer.valueOf(id));
        }
    }


    /**
     * 修改分管人员数据
     *
     * @param ykManageList
     */
    @Transactional
    @MysqlDataSource
    public void updateYkManageBatch(List<String> ykManageList, String healthFg) throws InvocationTargetException, IllegalAccessException {
        for (String id : ykManageList) {
            YkManage ykManage = new YkManage();
            ykManage.setId(Integer.valueOf(id));
            ykManage.setHealthFg(healthFg);
            ykManageDao.update(ykManage);
            YkManage ykManage1 = ykManageDao.getYkManageById(Integer.valueOf(id));
            ykBillService.updateFGYkBill(ykManage1);
            YkLog ykLog = new YkLog();
            BeanUtils.copyProperties(ykLog, ykManage1);
            ykLogDao.insert(ykLog);
        }
    }

    /**
     * 根据ID获取数据
     *
     * @param id
     */
    @Transactional
    @MysqlDataSource
    public YkManageView getYkManage(Integer id) {

        return ykManageDao.getYkManageById(id);
    }

    /**
     * 根据ID获取数据
     *
     *
     * @param cardId
     */
    @Transactional
    @MysqlDataSource
    public YkManageView getYkManageFind(String cardId) {

        return ykManageDao.getYkManageByFind(cardId);
    }

    /**
     * 根据ID获取数据
     *
     * @param billId
     */
    @Transactional
    @MysqlDataSource
    public YkManageView getYkManageBillID(String billId) {

        return ykManageDao.getYkManageBillId(billId);
    }

}